package com.yuyou.train.lesson.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.service.IResourceService;
import com.yuyou.train.lesson.vo.req.ReqResourceAddrUpdate;
import com.yuyou.train.lesson.vo.resp.RespResExamDetail;
import com.yuyou.train.lesson.vo.resp.RespResourceDetail;
import com.yuyou.train.service.api.lesson.dto.ExamDetailDTO;
import com.yuyou.train.service.api.lesson.dto.ResourceDetailDTO;
import com.yuyou.train.service.api.lesson.dto.ResourcePageDTO;
import com.yuyou.train.service.api.lesson.entity.Resource;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.tool.DefindBeanUtils;

@Validated
@RestController
@RequestMapping(value="/api/resource")
public class ResourceController extends BaseController{

	
	@Autowired
	private IResourceService resourceService;
	
	@Autowired
	private IResourceExamService resourceExamService;
	
	@ApiOperation(value="新增更新资源")
	@RequestMapping(value="/addOrUpdateRes",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addOrUpdateRes(HttpServletRequest request,@Validated @ModelAttribute ReqResourceAddrUpdate reqResourceAddrUpdate){			
		Resource resource=new Resource();	
		resource.setCreateUser(this.getUserObj(request).getUserId());
		DefindBeanUtils.copyProperties(reqResourceAddrUpdate, resource);	
		ResBoolSimpleInfo<String> res=resourceService.addOrUpdateRes(resource);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="删除资源")
	@ApiImplicitParam(name = "id", value = "资源ID", paramType="query", required = true)
	@RequestMapping(value="/delRes",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> delRes(HttpServletRequest request,
			@NotNull(message="id不能为空") String id){		
		ResBoolSimpleInfo<String> res=resourceService.delRes(id);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="资源详情")
	@ApiImplicitParam(name = "id", value = "资源ID", paramType="query", required = true)
	@RequestMapping(value="/detailRes",method=RequestMethod.POST)
	public ReturnResult<RespResourceDetail> detailRes(@NotNull(message="id不能为空") String id){	
		ResourceDetailDTO resource=resourceService.selectByPrimaryKey(id);
		RespResourceDetail res=new RespResourceDetail();
		DefindBeanUtils.copyProperties(resource, res);	
		if(!StringUtils.isEmpty(resource.getResourceExamId())){
			ExamDetailDTO resourceExam=resourceExamService.selectDetailByPrimaryKey(resource.getResourceExamId());
			RespResExamDetail rsExamDetail=new RespResExamDetail();
			DefindBeanUtils.copyProperties(resourceExam, rsExamDetail);	
			res.setRespResExamDetail(rsExamDetail);
		}	
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="资源分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "资源名称", paramType="query", required = false),
		@ApiImplicitParam(name = "resourceType", value = "资源类别：01：视频，02：音频，03：文件，04：试卷，05：作业（自评作业），06：作业（提交型作业），07：感想", paramType="query",required = false)}
		)
	@RequestMapping(value="/findResPage",method=RequestMethod.POST)
	public ReturnResult<ResourcePageDTO> findResPage(
			@Validated @ModelAttribute PageParams pageParams,
			String name,String resourceType){				
		Map<String, Object> conditions=new HashMap<String, Object>();
		conditions.put("name", name);
		conditions.put("resourceType", resourceType);
		Pagination<ResourcePageDTO> queryPgn = new Pagination<ResourcePageDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total=resourceService.findResPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<ResourcePageDTO> list=resourceService.findResPageList(queryPgn);		
			queryPgn.setList(list);
		}	
		return ReturnResult.success(queryPgn);
	}

	
	
}
